package com.lianzhihui.romeex.ui.ecological;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.NodeBuyAdapter;
import com.lianzhihui.romeex.bean.EcologicalBean;
import com.lianzhihui.romeex.bean.MnnerTransferListBean;
import com.lianzhihui.romeex.inter.DialogListener;
import com.lianzhihui.romeex.mvp.presenter.NodebuyPresnterImp;
import com.lianzhihui.romeex.mvp.view.NodeBuyView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.widget.dialog.ChoseIntegralDialog;
import com.lianzhihui.romeex.widget.dialog.ZhuanzhangDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.ecological
 * @ClassName: NodeBuyActivity
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/12/15 9:54
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/12/15 9:54
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class NodeBuyActivity extends BaseActivity implements NodeBuyView, OnRefreshLoadMoreListener {

    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    @BindView(R.id.myRecyclerView)
    RecyclerView myRecyclerView;
    //    @BindView(R.id.usdt_erc)
//    CardView usdt_erc;
//    @BindView(R.id.usdt_trc)
//    CardView usdt_trc;
//    @BindView(R.id.ret)
//    CardView ret;
    @BindView(R.id.buyed)
    CardView buyed;
    @BindView(R.id.buy_node_state)
    TextView buy_node_state;

    private NodebuyPresnterImp integralTransferPresnterImp;
    private final int pageSize = 20;
    private NodeBuyAdapter nodeBuyAdapter;
    private final int COUNT = 10000;
    private double inputNum = 0;
    private int buyType;
    private EcologicalBean bean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_node_buy;
    }

    @Override
    protected void initView() {

        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setStatusBarTextColor(this, false);
        includeTopLinBackground.setBackgroundColor(getResources().getColor(R.color.transparent));
        integralTransferPresnterImp = new NodebuyPresnterImp(this, this);
        nodeBuyAdapter = new NodeBuyAdapter(new ArrayList<>());
        nodeBuyAdapter.setEmptyView(getEmptyView());
        myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        myRecyclerView.setAdapter(nodeBuyAdapter);
        mySmartRefreshLayout.autoRefresh();
        mySmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        setTitle(getString(R.string.text_node_buy));
        integralTransferPresnterImp.requestMinnerMyBalance();
    }

    @Override
    public boolean setFitSystemWindowFlag() {
        return false;
    }

    @Override
    protected void initData() {


    }


    @OnClick({R.id.buyed})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
//                case R.id.buy_node_state:
//                    inputNum = 10000;
//                    buyType = 1;
//                    buy(10000);
//                    break;
//                case R.id.usdt_trc:
//                    inputNum = 10000;
//                    buyType = 2;
//                    buy(10000);
//                    break;
//                case R.id.ret:
//                    buyType = 3;
//                    if (bean != null) {
//                        inputNum = COUNT / bean.getRetCoinPrice();
//                    }
//                    buy(inputNum);
//                    break;
                case R.id.buyed:
                    if (bean == null) {
                        return;
                    }
                    ChoseIntegralDialog dialog = new ChoseIntegralDialog(this, bean);
                    dialog.setCancelable(false);
                    dialog.setDialogListener((type, count) -> {
                        buy(count, type);
                        dialog.dismiss();
                    });
                    dialog.bottom();
                    dialog.fullWidth();
                    dialog.show();
                    break;

                default:
                    break;
            }
        }
    }

    private void buy(String v1, String buyType) {

        ZhuanzhangDialog dialog = new ZhuanzhangDialog(this);
        if (buyType.equals("3")) {
            dialog.setSum(Double.parseDouble(v1), "RET");
        }else if (buyType.equals("2")) {
            dialog.setSum(Double.parseDouble(v1), "USDT(TRC20)");
        }else{
            dialog.setSum(Double.parseDouble(v1), "USDT(ERC20)");
        }

        dialog.setTitle(getString(R.string.text_buying_now));
        dialog.setTitle_trans(getString(R.string.text_sub_num));
        dialog.setListener(new DialogListener() {
            @Override
            public void onPassed() {
                showLoading("");
                integralTransferPresnterImp.requestMinnerBuyNode(buyType);
                dialog.dismiss();
            }

            @Override
            public void onFail() {
                toast("安全码错误");
            }
        });
        dialog.show();


    }


    @Override
    public void setFailure() {
        dismissLoading();
        mySmartRefreshLayout.finishLoadMore();
        mySmartRefreshLayout.finishRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        integralTransferPresnterImp.requestMinnerTransferList(pageNum, pageSize, "1");
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        integralTransferPresnterImp.requestMinnerTransferList(pageNum, pageSize, "1");
    }

    @Override
    public void responseMinnerTransferListSuccess(MnnerTransferListBean b) {
        if (b != null) {
            if (pageNum == 1) {
                nodeBuyAdapter.setNewData(b.getContent());
            } else {
                nodeBuyAdapter.addData(b.getContent());
            }
            pageNum++;
        }
        mySmartRefreshLayout.finishLoadMore();
        mySmartRefreshLayout.finishRefresh();
    }

    @Override
    public void responseMinnerMyBalanceSuccess(EcologicalBean b) {
        if (b != null) {
            bean = b;
            if (b.getMinnerLevel() == 1) {
                //已经购买
                buyed.setCardBackgroundColor(getResources().getColor(R.color.text_cccccc_color));
                buy_node_state.setText(getResources().getString(R.string.text_node_buyed));
                buyed.setEnabled(false);
            } else {
                buyed.setCardBackgroundColor(getResources().getColor(R.color.theme_color));
                buy_node_state.setText(getResources().getString(R.string.text_node_buy_now));
            }
        }
    }

    @Override
    public void responseMinnerBuyNodeSuccess() {
        dismissLoading();
        buyed.setCardBackgroundColor(getResources().getColor(R.color.text_cccccc_color));
        buy_node_state.setText(getResources().getString(R.string.text_node_buyed));
        buyed.setEnabled(false);
        mySmartRefreshLayout.autoRefresh();
        //已经购买
//        usdt_erc.setVisibility(View.GONE);
//        usdt_trc.setVisibility(View.GONE);
//        ret.setVisibility(View.GONE);
//        buyed.setVisibility(View.VISIBLE);
    }
}